/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Actor;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.UseCase;

import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Defines management of associations.
 * Redefines the EdgeAssociationHelper of the classdiagram package and specializes it for
 * use case diagrams.
 */
public class EdgeAssociationHelper extends com.cea.papyrus.diagram.classdiagram.helper.EdgeAssociationHelper {

	/** debug channel */
	private final int channel = 203;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		// first step of connection. target is null. source shall be a classifier (a type?)
		if ( (target==null) && (source instanceof  Classifier)) {
			return true;
		} else {
			// target is not null. Second part of the connection action
			if(source instanceof Actor) {
				// actor should only have association with Use case, component or classes.
				if(target instanceof UseCase || target instanceof Component || target instanceof org.eclipse.uml2.uml.Class) {
					return true;
				}
			} else 
			// target can be an actor: then, source should be USe CAse, class or component
			if(target instanceof Actor) {
				if(source instanceof UseCase || source instanceof Component || source instanceof org.eclipse.uml2.uml.Class) {
					return true;
				}
			} else 
				// all classifiers except actor (already corrected in previous cases)
				if(target instanceof Classifier ) { // classifier or type?
					// can not verify the following constraint:
					// UseCases cannot have Associations to UseCases specifying the same subject.
					return true;
			}
		}
		return false;
	}



	/**
	 * {@inheritDoc}
	 */
	@Override
	public GraphElement createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size){
		GraphElement ge = super.createGraphElement(umlChild, parent, location, size);

		// Role of AssociationEnd number2
		((GraphNode)ge.getContained().get(2)).setIsVisible(true);

		// Multiplicity of AssociationEnd number2
		((GraphNode)ge.getContained().get(3)).setIsVisible(false);

		return ge;		
	}	
}